class CreateActions < ActiveRecord::Migration
  def self.up
    create_table :actions do |t|
      t.column :name, :string
      t.column :description, :string
    end
    
    Action.create :name => "modify",  :description => "Add and edit pages. Review and rollback to previous revisions."
    Action.create :name => "publish", :description => "Publish and un-publish page revisions."
    Action.create :name => "remove",  :description => "Remove revisions and pages."
    Action.create :name => "manage",  :description => "Manage users."
    
    create_table "actions_user_types", :id => false do |t|
      t.column "action_id", :integer
      t.column "user_type_id", :integer
    end
    
    writer = UserType.find_by_name("Writer")
    writer.actions << Action.find_by_name("modify")
    writer.save
    
    publisher = UserType.find_by_name("Publisher")
    publisher.actions << Action.find_by_name("modify")
    publisher.actions << Action.find_by_name("publish")
    publisher.actions << Action.find_by_name("remove")
    publisher.save
    
    administrator = UserType.find_by_name("Administrator")
    administrator.actions << Action.find_by_name("modify")
    administrator.actions << Action.find_by_name("publish")
    administrator.actions << Action.find_by_name("remove")
    administrator.actions << Action.find_by_name("manage")
    administrator.save
  end

  def self.down
    drop_table :actions_user_types
    drop_table :actions
  end
end
